package com.js.service.bot.response.impl;

import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.js.common.annotation.ResponseClass;
import com.js.domian.entity.bot.ResponseEntity;
import com.js.domian.model.bot.analysis.MessageChain;
import com.js.service.bot.response.BaseService;
import org.springframework.stereotype.Service;

@Service
@ResponseClass("网络图片(一次重定向)")
public class WebImageRedirectOneService implements BaseService {

    @Override
    public MessageChain buildMessage(Long groupId, Long userId, String message, ResponseEntity response) {
        MessageChain msg = new MessageChain();
        if("1".equals(response.getAt())){
            msg.at(userId);
        }
        HttpRequest httpRequest = HttpUtil.createGet(response.getContent()).setMaxRedirectCount(1);
        httpRequest.execute();
        msg.image(URLUtil.normalize(httpRequest.getConnection().getUrl().toString()));
        return msg;
    }
}

